﻿//-----------------------------------------------------------------------
// <copyright file="PBSFeatureReceiver.cs" >
//     Copyright (c) Alon Havivi havivi@gmail.com. All rights reserved.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Globalization;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security; 

namespace PropertyBagSettings
{
    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
    class PBSFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {            
            SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent; 
            if (!webApp.IsAdministrationWebApplication) { 
                Guid featureId = properties.Feature.DefinitionId; 
                webApp.Features.Remove(featureId, true); 
                webApp.Update(); 
                throw new SPException(string.Format(CultureInfo.InvariantCulture, "You can activate the feature with ID {0} on the Central Administration Web Application only!", featureId)); 
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            //throw new NotImplementedException();
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new NotImplementedException();
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new NotImplementedException();
        }
    }

}
